window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		U.fillHead();
		U.fillFooter();
		var user = U.checkLogin();
		var poverty_id = U.getParam("poor_id");			//贫困者id
		var _crowdId = U.getParam("crowdId");				//众筹id
		var _crowdTitle = U.getParamUrl("title",decodeURI(document.location.search));		//众筹title
		var accountType =  U.getParam("accountType");
		if(accountType != null){
			accountType = 1;
		}else {
			accountType = 2;
		}
		var returnUrl;
		initPage();

		function initPage() { //初始化页面
			if(accountType==1){
				$(".menuBtn").css("border","none");
				$(".goods").css("display","none");
			}
			if(user.state == 0) {
				$(".help_person_info").show();
			}
			else {
				$(".help_person_info").hide();
			}
			if(_crowdTitle != null && _crowdTitle != ""){
				$(".title_text").show();
				$('.span-name').html(_crowdTitle);
				$(".des").hide();
			}
			$(".menuBtn>div:first-child").addClass("btnColor");
			U.loadOpt(false);
			/*if(U.getParam("poor_id") != null) {
			/!*	$(".entrance_poor_info").show();*!/
				U.ajaxget("/poverty/detail/" + U.getParam("poor_id"), {}, function(json) {
					U.loadOpt(false);
					if(json.success) {
						var povertyObjVO = json.models.povertyObjVO;
						poverty_id = povertyObjVO.id;
						/!*$(".poverty_name>span").text(povertyObjVO.name);
						$(".poverty_loc").text(povertyObjVO.address);

						$(".poverty_why").text(povertyObjVO.reason);*!/

					} else {
						M.toast(false, json.errorMsg);
					}
					if(poverty_id==null||poverty_id==""||poverty_id ==0){
						//jjh
						$("#dw").html("大爱平江扶贫助困慈善基金会");
					}else{
						//xh
						$("#dw").html("大爱平江扶贫助困慈善协会");
					}
				});
			}else{

			}*/
			initEvent();
		}

		function initEvent() {
			var title = "慈善捐款";
			var goodsType = 1;//1-捐款，2-捐物
			//var accountType = 2;//默认为协会1-基金会，2-协会
			var content = "";
			if(accountType == 1){
				//jjh
				$("#dw").html("大爱平江扶贫助困慈善基金会");
			}else{
				//xh
				$("#dw").html("大爱平江扶贫助困慈善协会");
			}
			/*$(".look").click(function() {
				U.laodPoor_view(true, {
					"id": U.getParam("poor_id")
				});
			});*/
			$(".menuBtn>div").click(function() {
				/*$(".menuBtn>div").css({
				/!*	"border": "1px solid #ff6600",*!/
					"color": "#ff6600",
					"background-color": "#ffffff"
				});
				$(this).css({
					"color": "#ffffff",
					"background-color": "#ff6600",
					"border-color": "#ff6600"
				});*/
				if($(this).text() == "捐款") {
					$(".goods").removeClass("btnColor");
					$(".money").addClass("btnColor");
					$(".help_money").show(500);
					$(".help_goods").hide();
					title = "慈善捐款";
					goodsType = 1;
					$('.look_type').show();
				} else {
					$(".money").removeClass("btnColor");
					$(".goods").addClass("btnColor");
					$(".help_money").hide();
					$(".help_goods").show(500);
					title = "慈善捐物";
					goodsType = 2;
					$('.look_type').hide();
				}
			});
			//$(".help_he_time input").width($(".content").width() - 100 - 150);
			$(".help_he_wz textarea").width($(".content").width() - 100 - 150);
			$(".union_pay").click(function(){
				$(".union_pay").attr("disabled","disabled");
				var donation;
				var amount = $("#amount").val();
				var goods = $("#help_content").val();                       //给予物质
				var reg=/^(-?\d*)\.?\d{1,2}$/;
				if(user.state == 0){										//未登录
					var _helpfulName;
					var _helpfulTel;
					//var _helpfulAddr;
					var regTel = /^(0\d{2,3}-?\d{7,8})|(1[34578]\d{9})$/;   //验证电话和手机号
					if (goodsType==1){
						_helpfulName = $(".helpfulName").val();
						_helpfulTel =$(".helpfulTel").val();
						//_helpfulAddr = $(".helpfulAddress").val();
						if(!reg.test(amount)||amount<=0){
							M.toast(false,'请输入大于0的整数或者保留两位小数');
							U.loadOpt(false);
							return false;
						}
						if(amount > 1000000){
							M.toast(false,'捐助金额不可超过100万');
							U.loadOpt(false);
							return false;
						}
						content = _helpfulName==null?"":_helpfulName + " " + title + " "+amount +" 元。";
						if(_helpfulName == ""){
							M.toast(false,'请输入姓名');
							U.loadOpt(false);
							return false;
						}
						if(_helpfulTel == ""){
							M.toast(false,'请输入电话');
							U.loadOpt(false);
							return false;
						}
						else if(!regTel.test(_helpfulTel)){
							M.toast(false,'请输入正确的电话');
							U.loadOpt(false);
							return false;
						}
						/*if(_helpfulAddr == ""){
							M.toast(false,'请输入地址');
							U.loadOpt(false);
							return false;
						}*/
					}
					else {
						_helpfulName = $(".helpful_name").val();
						_helpfulTel =$(".helpful_tel").val();
						//_helpfulAddr = $(".helpful_address").val();
						if(goods == ""){
							M.toast(false,'给予物质不能为空');
							U.loadOpt(false);
							return false;
						}
						if(_helpfulName == ""){
							M.toast(false,'请输入姓名');
							U.loadOpt(false);
							return false;
						}
						if(_helpfulTel == ""){
							M.toast(false,'请输入电话');
							U.loadOpt(false);
							return false;
						}
						else if(!regTel.test(_helpfulTel)){
							M.toast(false,'请输入正确的电话');
							U.loadOpt(false);
							return false;
						}
						/*if(_helpfulAddr == ""){
							M.toast(false,'请输入地址');
							U.loadOpt(false);
							return false;
						}*/
						amount = 0;
						content = _helpfulName + " " + title + "  "+$("#help_content").val();
					}
					//未登录时众筹捐助
					if(_crowdId != null && _crowdId != ""){
						donation={
							accountType: accountType,               //1-jjh,2-"xh",
							amount: accMul(amount,100),
							content: content,
							crowdId: _crowdId,         //众筹id
							device: "pc",
							goodsType: goodsType,                   //1捐款， 2捐物
							title: title,
							state: 1,
							payType: $("input[type='radio']:checked").val(),            //支付方式 1支付宝 ，2微信支付
							payer:_helpfulName,
							payMobile:_helpfulTel,
							payAddress:""
						}

					}
					//未登录时所有捐助
					else {
						donation={
							title:title,
							povertyId:poverty_id,
							payer:_helpfulName,
							payMobile:_helpfulTel,
							payAddress:"",
							content:content,
							state:1,
							amount:accMul(amount,100),
							payType:$("input[type='radio']:checked").val(),			//支付方式 1微信 2支付宝
							accountType:accountType,//1-jjh,2-"xh",
							goodsType:goodsType,
							device:"pc"
						};
					}
				}
				else{								//登录
					var name = user.info.personVO.name;
					if(goodsType==1){
						if(!reg.test(amount)||amount<=0){
							M.toast(false,'请输入大于0的整数或者保留两位小数');
							U.loadOpt(false);
							return false;
						}
						if(amount > 1000000){
							M.toast(false,'捐助金额不可超过100万');
							U.loadOpt(false);
							return false;
						}
						content = name==null?"":name + " " + title + " "+amount +" 元。";
					}
					else{
						if(goods == ""){
							M.toast(false,'给予物质不能为空');
							U.loadOpt(false);
							return false;
						}
						amount = 0;
						content = name==null?"":name + " " + title + "  "+$("#help_content").val();
					}
					//登录时众筹捐助
					if(_crowdId != null && _crowdId != ""){
						donation = {
							accountType: accountType, //1-jjh, 2-"xh",
							amount: accMul(amount,100),
							content: content,
							crowdId: _crowdId,
							device: "pc",
							goodsType: goodsType, 								//1捐款， 2捐物
							title: title,
							userId: user.info.id,
							state: 1,
							payType:$("input[type='radio']:checked").val()			//支付方式 1微信 2支付宝
						}
					}
					//登录时所有捐助
					else{
						donation={
							title:title,
							povertyId:poverty_id,
							userId:user.info.id,
							content:content,
							state:1,
							amount:accMul(amount,100),
							payType:$("input[type='radio']:checked").val(),			//支付方式 1微信 2支付宝
							accountType:accountType,//1-jjh,2-"xh",
							goodsType:goodsType,
							device:"pc"
						};
					}
				}
				if(poverty_id==null||poverty_id==""){
					accountType = 1;
				}
				U.ajax('donation/add', donation, function(json) {
					if(json.success){
						U.loadOpt(false);
						var result = json.models.donationVO;
						var sign=result.sign;
						var QRCode;
						returnUrl = "http://192.168.1.32:8081/donation/detail/"+result.id+"?device=pc";
						if(goodsType==1){
							if(result.payType==1){
								U.ajax("/alipay/topay",{"sign":sign},function(json){
									$("#topay").html(json.models.sHtmlText);
									$("#alipaysubmit").submit();
								});
							}
							if(result.payType == 2) {		//微信

								U.ajax('/../wxpay/toPcPay', {"sign":sign}, function(json) {
									var result=json.models.returStr;
									result=$.parseJSON( result);
									//console.log(result);
									//U.tipPay(QRCode);
									//result=$.parseJSON( result);
									$(".box").empty();
									QRCode=result.codeUrl;
									$("#shade").show();
									var QRHtml ='<script type="text/javascript" src="../js/jquery.js"></script><script type="text/javascript" src="../js/jquery.qrcode.min.js"></script>';
									QRHtml +="" +
										"<div id='confirm' style='width: 620px;height: 540px;position: fixed;top: 10%;left: 30%;z-index: 21;background-color: #fff;' >" +
											"<div class='title' style='width: 100%;height: 60px;;text-align: center;background-color: #EFEFEF;font-size: 18px;line-height: 60px;'>提示<img class='close-pay' src='../images/icons/wx-close.png' style='float: right;margin: auto 0;margin-top: 23px;margin-right: 17px;'></div>" +
											"<div style='width: 564px;height: 404px;background-color: #ffffff;padding-left:56px;' >" +
												"<div class='confirm-left' style='width: 218px;float: left;padding-top: 38px;'>" +
													"<div class='con-top'><span style='font-size: 18px;color: #333333;'>微信支付：</span><span style='color: #d30c1a;font-size: 18px;font-weight: bold;margin: 0 18px'>"+$("#amount").val()+"</span><span  style='font-size: 18px;color: #333333;'>元</span></div>" +
													"<div class='con-center' id='code' style='width: 215px;height: 215px'></div>" +
													"<div class='con-bottom' style='margin-top: 32px'>" +
														"<div style='width: 215px;height: 60px;margin: 0 auto;background-color: #d30c1a'>" +
															"<span style='float: left;    margin-left: 16px;'><img src='../images/icons/wx-sm.png' style='margin-top: 17px'></span>" +
															"<span style='font-size: 16px;width: 140px;color: #fff;margin-left: 16px;padding-left: 49px;display: block;padding-top: 12px;'>请使用微信扫一扫扫描二维码</span></div>" +
													"</div>" +
												"</div>" +
												"<div class='confirm-right' style='float: left;margin-left: 24px;padding-top: 82px'><img src='../images/icons/wx-dh.png'></div>" +
										"</div>";
									$(".box").append(QRHtml);
									$(".box").show();
									$("#code").css({
										"margin-top": "40px",
									});
									$(".close-pay").click(function(){
										$("#shade").hide();
										$("#confirm").hide();
									})
									//$("#code").qrcode(QRCode);
									$("#code").qrcode({
										width: 215, //宽度
										height:215, //高度
										text: QRCode//任意内容
									});
								});
							}
							/*if(json.models.donationVO.payType==3){
							 U.ajax("/unionpay/topay",{"sign":json.models.donationVO.sign},function(json){
							 $("#topay").html(json.models.sHtmlText);
							 $("#alipaysubmit").submit();
							 });
							 }*/
						}else{
							location.href="http://192.168.1.32:8081/donation/detail/"+json.models.donationVO.id+"?returnStr=success&device=pc";
						}
					}
					else {
						M.toast(false, json.errorMsg);
						U.loadOpt(false);
					}
				});
			});
			//取消支付
			$(".closePay").click(function(){
				$(".box").hide();
				$("#shade").hide();
				$(".makeCode").html("");
			});

			//支付完成
			$(".finishPay").click(function(){
				location.href=returnUrl;
			});
		}
	});
};